home *** CD-ROM | disk | FTP | other *** search
- /* qsort - quick sort routine */
-
- /* translated to C from the 'Software Tools in Pascal' routines
- by Kernighan & Plauger
- */
-
- /* qsort - quick sort routine */
- qsort(linepos,nlines,cmprec)
- char *linepos[]; int nlines; int (*cmprec)();
- {
- qsort1(linepos,0,nlines-1,cmprec);
- }
-
- /* qsort1 - the real sort routine */
- static qsort1(linepos,lo,hi,cmprec)
- char *linepos[]; int lo,hi; int (*cmprec)();
- {
- char *pivline;
- int i,j;
-
- /* make sure there's something to sort */
- if (lo >= hi)
- return;
-
- /* initialize */
- i = lo;
- j = hi;
- pivline = linepos[j];
-
- /* partition the set */
- do {
- while (i < j && (*cmprec)(linepos[i],pivline) <= 0)
- i++;
- while (j > i && (*cmprec)(linepos[j],pivline) >= 0)
- j--;
- if (i < j)
- exchange(linepos,i,j);
- } while (i < j);
-
- /* move pivot to i */
- exchange(linepos,i,hi);
-
- /* sort each partition */
- if (i - lo < hi - i) {
- qsort1(linepos,lo,i-1,cmprec);
- qsort1(linepos,i+1,hi,cmprec);
- }
- else {
- qsort1(linepos,i+1,hi,cmprec);
- qsort1(linepos,lo,i-1,cmprec);
- }
- }
-
- /* exchange - exchange two lines */
- static exchange(linepos,l1,l2)
- char *linepos[]; int l1,l2;
- {
- char *tmp;
-
- tmp = linepos[l1];
- linepos[l1] = linepos[l2];
- linepos[l2] = tmp;
- }
-